<?php
 class FormProcessor_ArticleEdit extends FormProcessor {
         static $tags = array(
         'a'   => array('href', 'target', 'name'),
         'img' => array('src', 'alt'),
         'b'   => array(),
         'strong' => array(),
         'em'  => array(),
         'i'   => array(),
         'ul'  => array(),
         'li'  => array(),
         'ol'  => array(),
         'p'   => array(),
         'br'  => array()
         );
         
         protected $db = null;
         public $user = null;
         public $article = null;
         
         public function __construct($db, $userid, $articleid = 0) {
                 parent::__construct();
                 $this->db = $db;
                 $this->user = new DatabaseObject_User($db);
                 $this->user->load($userid);
                 
                 $this->article = new DatabaseObject_Article($db);
                 $this->article->loadForUser($this->user->getId(), $articleid);
                 
                 if ($this->article->isSaved()) {
                         $this->subject = $this->article->subject;
                         $this->content = $this->article->content;
                         $this->dateline = $this->article->dateline;
                         if ($this->article->status > 0) {
                                 $this->status = '已发表';
                         } else {
                                 $this->status = '草稿';
                         }
                 } else {
                         $this->article->userid = $this->user->getId();
                 }
         }
         
         public function process(Zend_Controller_Request_Abstract $request) {
                 $this->subject = $this->sanitize($request->getPost('subject'));
                 $this->subject = substr($this->subject, 0, 255);
                 
                 if (strlen($this->subject) == 0) {
                         $this->addError('subject', '请输入文章标题');
                 }
                 //TODO 过滤文章内容：调用this->cleanHtml()
                 $this->content = $this->cleanHtml($request->getPost('content'));
                 
                 if (strlen($this->content) == 0) {
                         $this->addError('content', '请输入文章内容');
                 }
                 
                 if (!$this->hasError()) {
                         $this->article->subject = $this->subject;
                         $this->article->dateline = time();
                         $this->article->content = $this->content;
                         
                         $preview = !is_null($request->getPost('preview'));
                         if (!$preview) {
                                 $this->article->sendLive();
                         }
                         $this->article->save();
                 }
                 
                 return !$this->hasError();
                 
         }
         
         protected function cleanHtml($html) {
                 
                 $chain = new Zend_Filter();
                 $chain->addFilter(new Zend_Filter_StripTags(self::$tags));
                 $chain->addFilter(new Zend_Filter_StringTrim());
                 
                 $html = $chain->filter($html);
                 $tmp = $html;
                 while (1) {
                         $html = preg_replace('/(<[^>]*)javascript:([^>]*>)/i', '$1$2', $html);
                         if ($html == $tmp) {
                                 break;
                         }
                         $tmp = $html;
                 }
                 return $html;
                 return $html;
         }
         
 }
 ?>
